package com.daiqile.rxjava;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {
    public ApiService apiService;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initOkHttp();
        Retrofit retrofit = RetrofitClient.getInstance();
        apiService = retrofit.create(ApiService.class);
    }
    @OnClick({R.id.btn_in,R.id.btn_register,R.id.btn_hands})public void onClicked(View view){
        switch (view.getId()){
            case R.id.btn_in:
                Intent intent = new Intent(MainActivity.this,RecycleView.class);
                startActivity(intent);
                break;
            case R.id.btn_register:
                getSb();
                break;
            case R.id.btn_hands:
                Intent intent1 = new Intent(MainActivity.this,LockActivity.class);
                startActivity(intent1);
                break;
        }
    }
    private void getSb() {
        EditText editText = (EditText) findViewById(R.id.user_name_r);
        EditText editText1 = (EditText) findViewById(R.id.user_pwd_r);
        String username = editText.getText().toString().trim();
        String password = editText1.getText().toString().trim();
        final String time = EncryptUtils.getNowTimeStamp();
        final String public_key = EncryptUtils.createTimeStampToken(Long.valueOf(time), Constants.KEY_ONE);
        String first = "&time="+time+"&public_key="+ public_key;
        final String sign = EncryptUtils.params2Asc2Sort(first,Constants.KEY_TWO);
        Map<String, String> map = new HashMap<>();
        map.put("time",time);
        map.put("public_key",public_key);
        map.put("sign",sign);
        map.put("username",username );
        map.put("password", password);
        Log.d("time",time);
        Log.d("public_key",public_key);
        Log.d("sign",sign);
        Log.d("username",username);
        Log.d("password", password);
        apiService.user(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<User>() {
                    @Override
                    public void onCompleted() {
                    }
                    @Override
                    public void onError(Throwable e) {
                    }
                    @Override
                    public void onNext(User user) {
//                        int status = user.login_status();
//                        if (status==1){
//                            Toast.makeText(MainActivity.this,"success",Toast.LENGTH_SHORT).show();
//                        }else {
//                            Toast.makeText(MainActivity.this,"输入正确用户名",Toast.LENGTH_SHORT).show();
//
//                        }
                        if (user.login_status()==1){
                            Intent intent = new Intent(MainActivity.this,Else.class);
                            startActivity(intent);
                            Toast.makeText(MainActivity.this,"success",Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(MainActivity.this,"输入正确用户名",Toast.LENGTH_SHORT).show();

                        }
                    }
                });
    }


    private void initOkHttp() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
//                .addInterceptor(new LoggerInterceptor("TAG"))
                builder.connectTimeout(10000L, TimeUnit.MILLISECONDS)
                        .readTimeout(10000L, TimeUnit.MILLISECONDS)
                        //其他配置
                        .build();
    }


}
